package com.ruoyi.booking.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.ruoyi.landlord.mapper.HumanLandlordMapper;
import com.ruoyi.rental.domain.RentalAgreementsByName;
import com.ruoyi.room.mapper.HumanRoomMapper;
import com.ruoyi.user_list.mapper.MobileUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.ruoyi.booking.mapper.BookingMapper;
import com.ruoyi.booking.domain.Booking;
import com.ruoyi.booking.service.IBookingService;

/**
 * 预约管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-16
 */
@Service
public class BookingServiceImpl implements IBookingService 
{
    @Autowired
    private BookingMapper bookingMapper;

    @Autowired
    private MobileUserMapper mobileUserMapper;

    @Autowired
    private HumanRoomMapper humanRoomMapper;

    @Autowired
    private HumanLandlordMapper humanLandlordMapper;

    /**
     * 查询预约管理
     * 
     * @param bookingId 预约管理主键
     * @return 预约管理
     */
    @Override
    public Booking selectBookingByBookingId(Long bookingId)
    {
        return bookingMapper.selectBookingByBookingId(bookingId);
    }

    /**
     * 查询预约管理列表
     * 
     * @param booking 预约管理
     * @return 预约管理
     */
    @Override
    public List<Booking> selectBookingList(Booking booking)
    {
        return bookingMapper.selectBookingList(booking);
    }

    /**
     * 新增预约管理
     * 
     * @param booking 预约管理
     * @return 结果
     */
    @Override
    public int insertBooking(Booking booking)
    {
        booking.setUserName(mobileUserMapper.selectMobileUserNameByUserId(booking.getUserName()));
        booking.setLandlordName(humanLandlordMapper.selectHumanLandlordByLandlordName(booking.getLandlordName()));
        booking.setRoomNumber(humanRoomMapper.selectHumanRoomNameByRoomId(booking.getRoomNumber()));
        return bookingMapper.insertBooking(booking);
    }

    /**
     * 修改预约管理
     * 
     * @param booking 预约管理
     * @return 结果
     */
    @Override
    public int updateBooking(Booking booking)
    {
        return bookingMapper.updateBooking(booking);
    }

    /**
     * 批量删除预约管理
     * 
     * @param bookingIds 需要删除的预约管理主键
     * @return 结果
     */
    @Override
    public int deleteBookingByBookingIds(Long[] bookingIds)
    {
        return bookingMapper.deleteBookingByBookingIds(bookingIds);
    }

    /**
     * 删除预约管理信息
     * 
     * @param bookingId 预约管理主键
     * @return 结果
     */
    @Override
    public int deleteBookingByBookingId(Long bookingId)
    {
        return bookingMapper.deleteBookingByBookingId(bookingId);
    }
    @Override
    @Scheduled(cron = "0 0 0 1 * ?")
    public void checkExpiration() {
        List<Booking> list = bookingMapper.selectBookingList(new Booking());
        Calendar calendar = Calendar.getInstance();
        Date date=calendar.getTime();
        for (Booking booking : list) {
            if (date.after(booking.getBookingAt())){
                bookingMapper.deleteBookingByBookingId(booking.getBookingId());
            }
        }
    }
}
